package view.diaLogView;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.administrator.text.HomeActivity;
import com.example.administrator.text.R;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import data.SharedPreferencesUtils;
import util.BReceiverUtil.AlarmReceiver;
import util.GreenTreeNetworkUtil;
import util.HomeUtil;
import util.JumpActivityUtils;
import util.LogUtil;
import util.NetUtils;
import util.RequestDataCallback;
import util.StringUtil;
import util.Url;
import util.getInformation;

/**
 * Created by Administrator on 2016/6/30.
 * 标准的提示框
 * 0 表示退出  1 提示没有找到服务器 2连接的是4G网  3联网成功的提示 4联网失败的提示 5 代表已连接互联网
 * 6 授权成功 7断网成功 8代表续时成功  9提示关闭移动网络
 */
public class RetunDialog extends Dialog {
    private RetunDialog mRetunDialog;
    private UploadDialog mUploadDialog;
    private Context mContext;
    private String mFWQString;
    private HashMap mMap;
    private Logger mLogger = Logger.getLogger(RetunDialog.class);
    private boolean mJump; // true;点击其他区域点关闭diglog,false则不可以;
    private int mType;


    /*@Override
    public void show() {
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        super.show();
    }*/

    protected RetunDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public RetunDialog(Context context, String fwqString, int type, boolean jump) {
        super(context, R.style.upoad_dialog);
        this.mContext = context;
        this.mFWQString = fwqString;
        mRetunDialog = this;
        this.mType = type;
        this.mJump = jump;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.width = mContext.getResources().getDisplayMetrics().widthPixels;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mView = inflater.inflate(R.layout.retun_dialog, null);
        TextView textView1 = (TextView) mView.findViewById(R.id.text_retun_no);  //取消
        TextView textView2 = (TextView) mView.findViewById(R.id.text_retun_yes);//确定

        TextView textView3 = (TextView) mView.findViewById(R.id.text_retun_Name);         //提示信息的标题
        TextView textView4 = (TextView) mView.findViewById(R.id.text_retun_contentName);        //提示信息
        TextView textView5 = (TextView) mView.findViewById(R.id.text_retun_hint);              //单独一个的提示信息
        View view = mView.findViewById(R.id.view_dialog);
        LinearLayout linearLayout1 = (LinearLayout) mView.findViewById(R.id.linear_return1); //上区域
        LinearLayout linearLayout2 = (LinearLayout) mView.findViewById(R.id.linear_return2);//下区域
        LinearLayout linearHinet = (LinearLayout) mView.findViewById(R.id.linera_hint);    //确定和消失的布局

        if (mType == 0) {
            textView3.setVisibility(View.GONE);
            textView4.setVisibility(View.VISIBLE);textView5.setVisibility(View.GONE);linearHinet.setVisibility(View.VISIBLE);
            if(mType == 0){
                textView4.setText("退出应用可能会导致WiFi停止");
            }
            textView4.setTextSize(18);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            if(Build.VERSION.SDK_INT >=19){
                lp.setMargins(0,70, 0,70);
            }else {
                lp.setMargins(0,45, 0,45);
            }
            textView4.setLayoutParams(lp);
            view.setVisibility(View.VISIBLE);
        } else if (mType == 1) {
            textView3.setVisibility(View.VISIBLE);  linearHinet.setVisibility(View.VISIBLE);textView4.setVisibility(View.VISIBLE);
            textView3.setText("代理服务器没通");
            textView1.setText("网络诊断");
            textView4.setText("请检查网络连接，或者断开WiFi重新连接");
            textView4.setGravity(Gravity.LEFT);
            textView5.setVisibility(View.GONE);
            view.setVisibility(View.VISIBLE);
        } else if(mType == 2 || mType == 9){
            textView3.setVisibility(View.VISIBLE);  linearHinet.setVisibility(View.VISIBLE);textView4.setVisibility(View.VISIBLE);
            textView3.setText("欢迎使用牛郎网络");
            textView4.setText("您已经连接互联网,使用的是手机流量。可以尝试连接NewFi,享受更快更稳定的网络服务");
            if(mType == 2){
                textView3.setText("欢迎使用牛郎网络");
                textView4.setText("您已经连接互联网,使用的是手机流量。可以尝试连接NewFi,享受更快更稳定的网络服务");
            }else {
                textView3.setText("温馨提示");
                textView4.setText("建议您关闭移动网络开关,WiFi和移动网络开关同时打开时,有些手机会用移动网络数据");
            }
            textView4.setGravity(Gravity.LEFT);
            textView5.setVisibility(View.GONE);
            view.setVisibility(View.VISIBLE);
        }else if(mType == 3){
            textView5.setVisibility(View.VISIBLE);
            textView4.setVisibility(View.GONE);textView3.setVisibility(View.GONE);linearHinet.setVisibility(View.GONE);
            textView5.setText(mContext.getString(R.string.hint_wifi_yes));
        }else if(mType == 4  ||mType == 9){
            textView5.setVisibility(View.VISIBLE);
            textView4.setVisibility(View.GONE);textView3.setVisibility(View.GONE);
            textView5.setText("退出应用可能会导致WiFi停止");
            view.setVisibility(View.VISIBLE);
        }else if(mType == 5){
            textView5.setVisibility(View.VISIBLE);
            textView4.setVisibility(View.GONE);textView3.setVisibility(View.GONE);linearHinet.setVisibility(View.GONE);
            textView5.setText("已连接互联网");
        }else if(mType == 6){
            textView5.setVisibility(View.VISIBLE);
            textView4.setVisibility(View.GONE);textView3.setVisibility(View.GONE);linearHinet.setVisibility(View.GONE);
            textView5.setText("授权成功");
        }else if(mType == 7){
            textView5.setVisibility(View.VISIBLE);
            textView4.setVisibility(View.GONE);textView3.setVisibility(View.GONE);linearHinet.setVisibility(View.GONE);
            textView5.setText("断网成功");
        }else if(mType == 8){
            textView5.setVisibility(View.VISIBLE);
            textView4.setVisibility(View.GONE);textView3.setVisibility(View.GONE);linearHinet.setVisibility(View.GONE);
            textView5.setText("续时成功");
        }else if(mType == 9){

        }

        textView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mType == 0) {
                    gettextViewReturnData();
                } else if (mType == 1) {
                    mRetunDialog.dismiss();
                    if(mFWQString.equals("wel")){((Activity)mContext).finish();}
                    Intent mIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
                    mContext.startActivity(mIntent);
                }else if(mType == 2 || mType == 3){
                    mRetunDialog.dismiss();
                    Intent mIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
                    mContext.startActivity(mIntent);
                }else if(mType == 4){
                    mRetunDialog.dismiss();
                    JumpActivityUtils.getInstance().jumpSetNetFiItem(mContext);
                }else if(mType == 9){
                    mRetunDialog.dismiss();
                    Intent mIntent = new Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS);
                    mContext.startActivity(mIntent);
                }
            }
        });

        textView1.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                if(mType == 1){
                    JumpActivityUtils.getInstance().jumpPing(mContext);
                     mRetunDialog.dismiss();
                }else {
                    mRetunDialog.dismiss();
                }
            }
        });

        if (mJump) {
            HomeUtil.getHemeUtilNew().getDialogDismiss(linearLayout1, mRetunDialog);
            HomeUtil.getHemeUtilNew().getDialogDismiss(linearLayout2, mRetunDialog);
        }
        setContentView(mView, params);
    }

    /**
     * 退出登录的请求
     */
    private void gettextViewReturnData() {
        String url;
        mMap = new HashMap<>();
        mUploadDialog = new UploadDialog(mContext, "退出中");
        mUploadDialog.show();
        mRetunDialog.dismiss();
        String id = (String) SharedPreferencesUtils.getParam(mContext, HomeUtil.mUseridDB, "");
        String token = (String) SharedPreferencesUtils.getParam(mContext, HomeUtil.mTokenDB, "");
        mMap.put("userid", id);
        mMap.put("ip", getInformation.getNewGetInformation(mContext).getI_IP());
        String stringJson = StringUtil.getStringUtilNew().hashMapToJson(mMap); //参数包
        long time = StringUtil.getStringUtilNew().getTimeCurrent();
        String stirngSign = StringUtil.getStringUtilNew().getSign(Url.mStirngLoginout_loginOut, stringJson, token, id, time);
        Map map = StringUtil.getStringUtilNew().getMapNew(mContext,stringJson, token, id, Url.mStirngLoginout_loginOut, stirngSign, time);
        LogUtil.eE("fwq",mFWQString);
        if(NetUtils.getInstance(mContext).getNetWorkTeyt() == 0){
            url = Url.getUrlNew().getAppIP();
        }else {
            url = mFWQString;
        }

        mLogger.info("gettextViewReturnData()--->退出---fwq--"+url+"stringJson"+stringJson+"...NF_callid..."+map.get("NF_callid"));
        GreenTreeNetworkUtil.getInstance().doPost(mContext,url,3000,map, new RequestDataCallback() {
            @Override
            public void onSuccess(String response) {
                try {
                    mLogger.info("gettextViewReturnData()--->onSuccess()---String--"+response);
                    JSONObject jsonobject = new JSONObject(response);
                    LogUtil.eE("", response);
                    mUploadDialog.dismiss();
                    getRetunDilog();
                    Toast.makeText(mContext,"成功退出", Toast.LENGTH_SHORT).show();
                    StringUtil.getStringUtilNew().clearPhshData(mContext,0);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override public void onFailure(Throwable error) {
                getRetunDilog();
                Toast.makeText(mContext,"成功退出", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 退出的相关操作
     */
    private void getRetunDilog() {
        //退出融云（注销）
        /*AltairIM.getInstance().logout();*/
        SharedPreferencesUtils.setParam(mContext, HomeUtil.mTokenDB, "null");
        SharedPreferencesUtils.setParam(mContext, HomeUtil.mUseridDB, "0");
        Intent intent = new Intent(HomeActivity.mAction);
        intent.putExtra("wifi","finish");
        mContext.sendBroadcast(intent);        //发送一个广播
        ((Activity) mContext).finish();
        Intent intent1 = new Intent(mContext,AlarmReceiver.class);
        intent1.putExtra("networking","updateNetwork6");
        mContext.sendBroadcast(intent1);
    }

}
